﻿using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Text;

namespace Redis简单的链接
{
    public class RedisHelp单例
    {

        private static string RedisConnectionString = "192.168.190.130:6401";
        private static readonly object Locker = new object();
        private static ConnectionMultiplexer _instance;

        /// <summary>
        /// 单例获取
        /// </summary>
        public static ConnectionMultiplexer Instance
        {
            get
            {

                if (_instance == null)
                {
                    lock (Locker)
                    {
                        if (_instance == null || !_instance.IsConnected)
                        {
                            try
                            {
                                _instance = GetManager();
                            }
                            catch (Exception ex)
                            {
                                throw new Exception("初始化Redis缓存错误," + ex.Message);
                            }
                        }
                    }
                }
                return _instance;

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        private static ConnectionMultiplexer GetManager()
        {
            var options = ConfigurationOptions.Parse(RedisConnectionString);
            options.AbortOnConnectFail = false;
            var connect = ConnectionMultiplexer.Connect(options);
            return connect;
        }



    }
}
